package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.cartoonavatarmaker.lib.core.AvatarTemplateCategory;
import com.stark.cartoonavatarmaker.lib.core.AvatarTemplateProvider;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.TemplateAdapter;
import flc.ast.adapter.TypeAdapter;
import flc.ast.databinding.ActivityTemplateBinding;
import stark.common.basic.event.EventStatProxy;

public class TemplateActivity extends BaseAc<ActivityTemplateBinding> {

    public static int selPosition;
    private TypeAdapter typeAdapter;
    private TemplateAdapter templateAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_template;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivTemplateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvTypeList.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeAdapter = new TypeAdapter();
        mDataBinding.rvTypeList.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(this);

        mDataBinding.rvTemplateList.setLayoutManager(new GridLayoutManager(mContext, 2));
        templateAdapter = new TemplateAdapter();
        mDataBinding.rvTemplateList.setAdapter(templateAdapter);
        templateAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == typeAdapter) {
            selPosition = position;
            typeAdapter.setSelPosition(selPosition);
            typeAdapter.notifyDataSetChanged();
            setData();
        } else if (adapter == templateAdapter) {
            DressUpActivity.start(mContext,templateAdapter.getItem(position).part);
        }
    }

    @Override
    protected void initData() {
        List<AvatarTemplateCategory> listCategories = AvatarTemplateProvider.getCategories();
        typeAdapter.setList(listCategories);
        typeAdapter.setSelPosition(selPosition);
        typeAdapter.notifyDataSetChanged();
        setData();
    }

    private void setData() {
        templateAdapter.setList(typeAdapter.getItem(selPosition).templates);
    }

}